#include "BlockQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <unistd.h>


void *Consumer(void *args)
{
    BlockQueue<Task> *bq=static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        Task t=bq->pop();
        t.RunTask();
        //cout<<"执行了一个任务"<<endl;
        sleep(1);
    }
    //cout<<"Consumer create success"<<endl;
    return nullptr;
}


void *Productor(void *args)
{   
    //string tmp=;
    while(1)
    {
        int tmp=rand()%100;
        //string tmp("Taks num is ");
        //tmp+="rand()%100";
        Task t("Taks num is "+to_string(tmp));
        BlockQueue<Task> *bq=static_cast<BlockQueue<Task>*>(args);
        bq->push(t);
        cout << "发送了一个任务: " <<tmp<< endl;
        sleep(1);
    }
    //cout<<"Productor create success"<<endl;
    return nullptr;
}


int main()
{
    srand(time(nullptr));
    BlockQueue<Task> Bq;
    pthread_t c[3];
    pthread_t p[5];
    for(int i=0;i<3;i++)
    {
        pthread_create(c+i,nullptr,Consumer,&Bq);
    }

    for(int i=0;i<5;i++)
    {
        pthread_create(p+i,nullptr,Productor,&Bq);
    }

    for(int i=0;i<3;i++)
    {
        pthread_join(c[i],nullptr);
    }

    for(int i=0;i<5;i++)
    {
        pthread_join(p[i],nullptr);
    }
    return 0;
}